<template>
  <div class="autumn-container">
    <!-- 面包 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/dashboard' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/flow/category/categoryList' }">分类列表</el-breadcrumb-item>
      <el-breadcrumb-item>分类信息</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片 -->
    <div class="autumn-center">
      <div class="edit-form">
        <el-form ref="editFormRef" size="large" label-width="90px" :model="editForm" :rules="editFormRules">
          <el-row>
            <el-col :span="12">
              <el-form-item label="上级" prop="parentId">
                <autumn-tree-select size="large" :data="treeCategorys" :defaultKey="editForm.parentId"
                  @getValue="getParentId" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="名称" prop="name">
                <el-input v-model="editForm.name" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="排序" prop="sort">
                <el-input-number v-model="editForm.sort" :min="1" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="备注" prop="remark">
                <el-input type="textarea" resize="none" v-model="editForm.remark" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>

        <div class="form-foot-button">
          <el-button type="primary" @click="submit(editFormRef)">保存</el-button>
          <el-button @click="cancel">取消</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { FormInstance } from 'element-plus'
import { listCategory, viewCategory, saveCategory, updateCategory } from 'src/api/flow/category'

const treeCategorys = ref<Array<any>>([])
listCategory().then(({ data }) => {
  treeCategorys.value = data
})

// 定义表单数据
const editFormRef = ref<FormInstance>()
const editForm = reactive({
  id: '',
  parentId: '',
  name: '',
  sort: 1,
  code: '',
  remark: ''
})
const editFormRules = {
  name: [
    { required: true, message: '请输入分类名称', trigger: 'blur' }
  ],
  code: [
    { required: true, message: '请输入分类编码', trigger: 'blur' }
  ]
}

// 修改时填充表单数据
const route = useRoute()
if (route.query.id) {
  const id = route.query.id as string
  viewCategory(id).then(({ data }) => Object.assign(editForm, data))
}

// 获取上级分类
const getParentId = function (val: string) {
  editForm.parentId = val
}

// 提交
const submit = function (formEl: FormInstance | undefined) {
  if (!formEl) return
  formEl.validate(valid => {
    if (!valid) return
    if (!editForm.id) {
      saveCategory(editForm).then(() => {
        cancel()
      })
    } else {
      updateCategory(editForm).then(() => {
        cancel()
      })
    }
  })
}

// 关闭
const router = useRouter()
const cancel = function () {
  router.push('/flow/category/categoryList')
}
</script>

<style lang="scss" scoped>
</style>
